﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.ComponentModel;

namespace P2PTransmit.Modal
{
    public enum TransmitReceiveCode
    {
        UserRefuse,
        UserDiskSpaceNotEnough,
        UserAccept
    }
    public enum NetworkState
    {
        WaitForAccept,
        Transmitting,
        Pause,
        Error,
        Finish
    }
    public enum IOControl
    {
        Pause,
        Cancel,
        Start,
        Seek
    }
    public class NetworkTask : INotifyPropertyChanged,IEquatable<NetworkTask>,IComparer<NetworkTask>,IComparable
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public NetworkTask()
        {

        }

        public NetworkTask(object dropInfo, int n)
        {
            var list = new List<string>( 8 );
            if ( dropInfo is string )
            {
                var u = new Uri( dropInfo.ToString() );
                if ( u.IsFile )
                    list.Add( dropInfo.ToString() );
            }
            else
            {
                var files = dropInfo as string[];
                foreach ( var i in files )
                {
                    var u = new Uri( i );
                    if ( u.IsFile )
                        list.Add( i.ToString() );
                }
            }
            Files = new List<FileBudle>();
            foreach ( var i in list )
            {
                var fb = new FileBudle( i );
                Files.Add( fb );
                TotalFileLength += fb.FileLength;
            }
            FileCount = list.Count;
            list.Clear();
            list = null;
        }

        public string TaskName { get; set; }
        public int FileCount { get; set; }
        public long TotalFileLength { get; set; }

        string _currentFileName;
        public string CurrentFileName {
            get => _currentFileName;
            set {
                _currentFileName = value;
                PropertyChanged?.Invoke( this, new PropertyChangedEventArgs( nameof( CurrentFileName ) ) );
            }
        }

        long _currentLength;
        public long CurrentFileLength {
            get => _currentLength;
            set {
                _currentLength = value;
                PropertyChanged?.Invoke( this, new PropertyChangedEventArgs( nameof( CurrentFileLength ) ) );
            }
        }

        int _currentIndex;
        public int CurrentFileIndex {
            get => _currentIndex;
            set {
                _currentIndex = value;
                PropertyChanged?.Invoke( this, new PropertyChangedEventArgs( nameof( CurrentFileIndex ) ) );
            }
        }

        long _currentPosition;
        public long CurrentPosition {
            get => _currentPosition;
            set {
                _currentPosition = value;
                PropertyChanged?.Invoke( this, new PropertyChangedEventArgs( nameof( CurrentPosition ) ) );
            }
        }

        public long SpeedTime;

        long _speed;
        public long Speed { get => _speed;
            set {
                _speed = value;
                PropertyChanged?.Invoke( this, new PropertyChangedEventArgs( nameof( Speed ) ) );
            }
        }
        NetworkState _State;
        public NetworkState State {
            get => _State;
            set {
                _State = value;
                PropertyChanged?.Invoke( this, new PropertyChangedEventArgs( nameof( State ) ) );
            }
        }

        double _ping;
        public double Ping {
            get => _ping;
            set {
                _ping = value;
                PropertyChanged?.Invoke( this, new PropertyChangedEventArgs( nameof( Ping ) ) );
            }
        }

        public long StartStamp { get; set; }       

        public List<FileBudle> Files { get; set; }

        public string IPAddress { get; set; }

        public int Port { get; set; }

        public string Hash { get; set; }

        public bool Equals(NetworkTask other)
        {
            return this.Hash.Equals( other.Hash );
        }

        public int Compare(NetworkTask x, NetworkTask y)
        {
            return string.Compare( x.Hash, y.Hash );
        }

        public int CompareTo(object obj)
        {
            return Hash.CompareTo( obj );
        }
    }
    public class FileBudle
    {
        public FileBudle()
        {

        }
        public FileBudle(string file)
        {
            FilePath = file;
            var f = new System.IO.FileInfo( file );
            FileLength = f.Length;
            FileName = f.Name;
        }

        public string FileName { get; set; }
        public string FilePath { get; set; }
        public long FileLength { get; set; }
    }
}
